package com.zlm.common.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: duyufu
 * @Date: 2019/4/14 10:26
 */
public enum NewsTypeEnum {
    COMPANY_NEWS(1,"公司新闻"),
    INDUSTRY_NEWS(2,"行业资讯"),
    BROADCAST_NEWS(3,"播报新闻");

    private Integer code;

    private String name;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private NewsTypeEnum(Integer code,String name){
        this.code=code;
        this.name=name;
    }

    public static List<Map<String,Object>> getNewsType(){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map;
        for(NewsTypeEnum typeEnum:NewsTypeEnum.values()){
            map= new HashMap<String, Object>();
            map.put("code", typeEnum.getCode());
            map.put("name", typeEnum.getName());
            list.add(map);
        }
        return list;
    }

    public static String getNewsTypeJsonString(){
        StringBuilder builder = new StringBuilder("{");
        for(NewsTypeEnum typeEnum:NewsTypeEnum.values()){
            builder.append("\""+typeEnum.getCode()+"\":\""+ typeEnum.getName()+"\",");
        }
        String json=builder.substring(0, builder.length()-1)+"}";
        return json;
    }

    /**
     * 根据code得到name
     * @param code
     * @return
     */
    public static String getNameByCode(Integer code){
        for(NewsTypeEnum typeEnum:NewsTypeEnum.values()){
            if(code.equals(typeEnum.getCode())){
                return typeEnum.getName();
            }
        }
        return null;

    }

    @Override
    public String toString(){
        return "[code=" + code + ", name=" + name + "]";
    }

}
